package com.huarui.partner.dao.impl;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.ewaf.framework.dao.impl.BaseDaoImpl;
import org.springframework.orm.ibatis.SqlMapClientCallback;

import com.huarui.partner.dao.IPartnerDao;
import com.huarui.partner.model.Partner;
import com.ibatis.sqlmap.client.SqlMapExecutor;

;

/**
 * 客户daoImpl
 * 
 * @author ycf
 * 
 */
public class PartnerDaoImpl extends BaseDaoImpl implements IPartnerDao {

	@Override
	public void deletePartner(Partner ptr) {
		this.getSqlMapClientTemplate().update(
				super.namespace + ".deletePartner", ptr);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<Partner> getPartnersByLikeName(String name) {
		return this.getSqlMapClientTemplate().queryForList(
				super.namespace + ".getByLikeName", name);
	}

	@Override
	public boolean validatePartnerName(String name) {
		Integer num = (Integer) this.getSqlMapClientTemplate().queryForObject(
				super.namespace + ".getCountByName", name);
		if (num == 0)
			return false;
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Partner> getPartnerByOwnerId(Integer ownerId) {

		return getSqlMapClientTemplate().queryForList(
				namespace + ".getPartnerByOwnerId", ownerId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Partner> getAllPartnerByOwnerId(Map map) {
		 List<Partner>  partners=getSqlMapClientTemplate().queryForList(
				namespace + ".getAllPartnerByOwnerId", map);
		 return partners;
	}

	@SuppressWarnings("unchecked")
	@Override
	public int countByConditions(Map map) {

		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".countByConditions", map);
	}

	@Override
	public Partner getPartnerByCode(String code) {
		// TODO Auto-generated method stub
		return (Partner) getSqlMapClientTemplate().queryForObject(
				namespace + ".getPartnerByCode", code);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Partner> getPartnerByOwnerIdaAndLikeName(Map map) {

		return getSqlMapClientTemplate().queryForList(
				namespace + ".getPartnerByOwnerIdaAndLikeName", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public int getCountByConditions(Map map) {
		return (Integer) getSqlMapClientTemplate().queryForObject(
				namespace + ".getCountByConditions", map);
	}

	@SuppressWarnings("unchecked")
	public List<Partner> getPartnersByConditions(Map map) {
		return this.getSqlMapClientTemplate().queryForList(
				namespace + ".getPartnersByConditions", map);
	}

	@Override
	public void updatePartnerAmountById(Partner partner) {
		getSqlMapClientTemplate().update(
				namespace + ".updatePartnerAmountById", partner);

	}

	@Override
	public void batchUpdatePartner(final List<Partner> partners) {
		getSqlMapClientTemplate().execute(new SqlMapClientCallback() {
			
			@Override
			public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
				executor.startBatch();
				for(Partner partner : partners){
					executor.update(namespace+".updatePartnerAmountById",partner);					
				}
			    executor.executeBatch();
			    return null;
			}
		});
	}
	

	@SuppressWarnings("unchecked")
	@Override
	public List<Partner> getPartnerByIdent(Map map) {
		
		return (List<Partner>)getSqlMapClientTemplate().queryForList(namespace + ".getPartnerByIdent", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Partner getPartnerByTaxCode(Map map) {

		return (Partner)getSqlMapClientTemplate().queryForObject(namespace + ".getPartnerByTaxCode", map);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Partner findPartnerByOwnerId(Map map){
		
		return (Partner)getSqlMapClientTemplate().queryForObject(namespace + ".findPartnerByOwnerId", map);
	}
	

	@Override
	@SuppressWarnings("unchecked")
	public Map<String, Object> getByIdAndOwnerId(Map<String, Object> paramsMap) {
		return (Map<String, Object>) this.getSqlMapClientTemplate().queryForObject(super.namespace + ".getByIdAndOwnerId", paramsMap);
	}

	@Override
	public Partner getByIdForUpdate(Integer id) {
	
		return  (Partner) getSqlMapClientTemplate().queryForObject(namespace+".getByIdForUpdate", id);
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Partner> findPartnerByCondition(Map map){		
		
		return ( List<Partner> )getSqlMapClientTemplate().queryForList(namespace + ".findPartnerByCondition", map);	
		
	}

	@Override
	public boolean updatePartner(Partner partner) {

		return getSqlMapClientTemplate().update(namespace + ".updateCompany", partner) == 1;
	}
}
